package com.cf.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class PowerManage {

	public static <T> List<T> filterList(List<T> listT, int power, String department, String truename, String deptField,
			String userNameField) {
		deptField = "Department";
		userNameField = "UserTrueName";
		T t = null;
		List<T> listT1 = new ArrayList<T>();
		Class<? extends Object> tClass = t.getClass();
//        //得到所有属性
//        Field[] fields = tClass.getDeclaredFields();

		Method md = null;
		Method mn = null;
		try {
			md = tClass.getMethod("get" + deptField);
			mn = tClass.getMethod("get" + userNameField);
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 调用这个整合出来的get方法，强转成自己需要的类型
		// Integer id = (Integer)m.invoke(t);
		switch (power) {
		case 3:
			listT1 = listT;
			break;
		case 2:
			// t1=listT.stream().filter(f->f.getClass().getName()=department).collect(Collectors.toList());
			for (T t2 : listT) {
				try {
					if (md.invoke(t2).toString() == department)
						listT1.add(t2);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			break;
		case 1:
			for (T t2 : listT) {
				try {
					if (mn.invoke(t2).toString() == truename)
						listT1.add(t2);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		default:
			break;
		}

		return listT1;

	}

}
